package com.foxaweb.pageflip
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class Page extends Sprite {
		
		public var _width:Number;
		public var _height:Number;
		
		private var shape:Shape;
		private var bmp0:BitmapData;
		private var bmp1:BitmapData;
		private var _eventActivate:Boolean;
		
		public function Page(shape:Shape,bmp0:BitmapData, bmp1:BitmapData){
			this.shape = shape;
			this.bmp0 = bmp0;
			this.bmp1 = bmp1;
			addChild(this.shape);
			_width = width;
			_height = height;
			
			addEventListener(Event.ADDED, initListener);
			//this.traceLimitFlip();
		}
		
		private function initListener(evt:Event):void{
			addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			addEventListener(MouseEvent.MOUSE_OVER, onPress);
			addEventListener(MouseEvent.MOUSE_UP, 	onRelease);
			addEventListener(MouseEvent.MOUSE_OUT, 	onMouseOut);
			addEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		private function removeListener():void{
			removeEventListener(MouseEvent.MOUSE_DOWN, 	onPress)
			removeEventListener(MouseEvent.MOUSE_OVER, 	onPress);
			removeEventListener(MouseEvent.MOUSE_UP, 	onRelease);
			removeEventListener(MouseEvent.MOUSE_OUT, 	onMouseOut);
			removeEventListener(MouseEvent.MOUSE_MOVE, 	onMove);
		}
		
		private function onPress(evt:MouseEvent):void{
			if(!evt.buttonDown) return;
			this.dispatchEvent(new Event("pageSelected"));
			updatePage(this.mouseX,this.mouseY);
			
		}
		
		private function onRelease(evt:MouseEvent):void{
			
			var posX:Number = (this.mouseX > this._width /2) ? this._width : 0;
			var posY:Number = this._height;
			updatePage(posX,posY);
			this.dispatchEvent(new Event("pageDeselected"));
											
		}
		private function onMouseOut(evt:MouseEvent):void{
			if(!evt.buttonDown) return;
			
			onRelease(evt);
		}		
		
		private function onMove(evt:MouseEvent):void{
			if(!evt.buttonDown) return;
			
			updatePage(this.mouseX,this.mouseY);
		}
		
		private function updatePage(posX:Number,posY:Number):void{
			posY = (posY < limitY(posX)) ? limitY(posX) : posY;
			shape.graphics.clear();
			var flip:Object = PageFlip.computeFlip(	new Point(posX,posY),
													new Point(1,1),
													bmp0.width,
													bmp0.height,
													true,
													1);
			PageFlip.drawBitmapSheet(flip, shape, bmp0 ,bmp1);	
		}
		
		private function traceLimitFlip():void{
			var posX:Number;
			var posY:Number;
			graphics.lineStyle(0,0x000000);
			for(var i:Number = 0;i<this._width;i++){
				posX = i;
				posY = limitY(posX);
				graphics.drawCircle(posX,posY,1);				
			}
			//trace(this._width,this._height,this._width/2); 
		}
		
		private function limitY(posX:Number):Number{
			return ((Math.pow((posX-(this._width/2)),2) / (this._width/2)) + (this._height-(this._width/2)))
		}
		
		/*
		 *	Getters & Setters
		 */ 
		 
		public function set eventActivate(boolean:Boolean):void{
			if(boolean){
				if(!_eventActivate)	initListener(new Event(Event.ADDED));
			}else{
				if(!_eventActivate)	removeListener();
			}
		}
		
		public function get eventActivate():Boolean{
			return _eventActivate;
		}
	}
}